const { query } = require('./db')

async function getArticleList(page, pageSize) {
    const sql = `SELECT * FROM article LIMIT ? , ?`
    const params = [(page - 1) * pageSize, pageSize]
    const result = await query(sql, params)
    return result
}

async function getArticleById(id) {
    const sql = `SELECT * FROM article WHERE id =?`
    const params = [id]
    const result = await query(sql, params)
    return result[0]
}

async function createArticle(title, content) {
    const sql = `INSERT INTO article (title, content) VALUES (?,?)`
    const params = [title, content]
    const result = await query(sql, params)
    return result.insertId
}

async function updateArticle(id, title, content) {
    const sql = `UPDATE article SET title =?, content =? WHERE id =?`
    const params = [title, content, id]
    const result = await query(sql, params)
    return result.affectedRows
}

async function deleteArticle(id) {
    const sql = `DELETE FROM article WHERE id =?`
    const params = [id]
    const result = await query(sql, params)
    return result.affectedRows
}

module.exports = {
    getArticleList,
    getArticleById,
    createArticle,
    updateArticle,
    deleteArticle
}